package com.gubei.oss.config;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.gubei.oss.adapter.AliStorageAdapter;
import com.gubei.oss.adapter.MinioStorageAdapter;
import com.gubei.oss.adapter.StorageAdapter;
import com.gubei.oss.adapter.StorageAdapterFactory;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * ファイル保存配置
 */
@Configuration
@RefreshScope
public class StorageConfig {

    @Value(value = "${storage.service.type}")
    private String storageType;

//    @Resource
//    private AliStorageAdapter aliStorageAdapter;
//
//    @Resource
//    private MinioStorageAdapter minioStorageAdapter;

    @Resource
    private StorageAdapterFactory storageAdapterFactory;

//    @Bean
//    @RefreshScope
//    public StorageAdapter storageService() {
//        if ("minio".equals(storageType)) {
//            return new MinioStorageAdapter();
////            return minioStorageAdapter;
//        } else if ("aliyun".equals(storageType)) {
//            return new AliStorageAdapter();
////            return aliStorageAdapter;
//        } else {
//            throw  new IllegalArgumentException("未找到对应的文件存储处理器");
//        }
//    }

    //工厂模式
    @Bean
    @RefreshScope
    public StorageAdapter storageService(){
        StorageAdapter storageAdapter = storageAdapterFactory.getStorageAdapter(storageType);
        if (storageAdapter != null) {
            return storageAdapter;
        } else {
            throw new IllegalArgumentException("未找到对应的文件存储处理器");
        }
    }
}
